package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import dao.Connector;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Customer {

	Connection connect =null;
	PreparedStatement pstm=null;
	private String table="tblCustomer";
	public int addCustomer(String name,String address, 
							int age, int gender, String country,
							int id_card,int id_team,String phone
							){
		connect = Connector.getConnect();
		try{
			pstm= connect.prepareStatement("INSERT INTO "+
					this.table+"(name,address,age,gender" +
					",nation,idcard,idteam,phone)" +
					" VALUES (?,?,?,?,?,?,?,?)",pstm.RETURN_GENERATED_KEYS);
			pstm.setString(1,name);
			pstm.setString(2,address);
			pstm.setInt(3,age);
			pstm.setInt(4,gender);
			pstm.setString(5,country);
			pstm.setInt(6,id_card);
			pstm.setInt(7,id_team);
			pstm.setString(8, phone);
			if(pstm.executeUpdate()>0){
                                pstm.getGeneratedKeys();
				ResultSet rs=pstm.getGeneratedKeys();
				if (rs.next()) {
				    return rs.getInt(1);
				}
				
			}
			
		}catch(SQLException e){
			e.printStackTrace();
		}
		return 0;
		
	}
	public boolean deleteCustomer(int id){
		connect = Connector.getConnect();
		try{
			pstm = connect.prepareStatement("DELETE FROM "+this.table+ " WHERE id = ?");
			pstm.setInt(1, id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return false;
	}
	public boolean updateCustomer(int id, String name,String address, 
			int age, int gender, String country,
			int id_card,int id_team,String phone){
		connect= Connector.getConnect();
		try{
			pstm = connect.prepareStatement("UPDATE "+this.table +" SET name=?," +
                                                        "address=?,age=?," +
                                                        "gender=?,nation=?,idcard=?,idteam=?," +
                                                        "phone=?  WHERE id=?");
			pstm.setString(1,name);
			pstm.setString(2,address);
			pstm.setInt(3,age);
			pstm.setInt(4,gender);
			pstm.setString(5,country);
			pstm.setInt(6,id_card);
			pstm.setInt(7,id_team);
			pstm.setString(8, phone);
			pstm.setInt(9,id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			
		}
		return false;
	}
	public ArrayList<entity.Customer> getAllCustomer(){
		connect= Connector.getConnect();
		ArrayList<entity.Customer> list = new ArrayList<entity.Customer>();
		ResultSet rs=null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table);
			rs=pstm.executeQuery();
                        InfoBooking info;
			while(rs.next()){
				list.add(this.setCustomer(rs)); 
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		return list;
	}
	public entity.Customer getCustomerById(int id){
		
		connect= Connector.getConnect();
		ResultSet rs;
                entity.Customer customer=null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table+" where id=?");
			pstm.setInt(1, id);
			rs= pstm.executeQuery();
                        if(rs.next()){
                           return this.setCustomer(rs);  
                        }
			// Lay thong tin gui den Entity
		}catch(SQLException e){
			e.printStackTrace();
		}
                return customer;
	}
	
	public ArrayList<entity.Customer> getCustomerbyTeam(int id){
        	connect= Connector.getConnect();
		ArrayList<entity.Customer> list = new ArrayList<entity.Customer>();
		ResultSet rs=null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table+" where idteam=?");
			rs=pstm.executeQuery();
			while(rs.next()){
				list.add(this.setCustomer(rs)); 
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		return list;
        
        }
        public ArrayList<entity.Customer> getCustomerbyStatus(int status){
        	connect= Connector.getConnect();
		ArrayList<entity.Customer> list = new ArrayList<entity.Customer>();
		ResultSet rs=null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table+" where idteam=?");
			rs=pstm.executeQuery();
			while(rs.next()){
				list.add(this.setCustomer(rs)); 
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		return list;
        
        }
        public ArrayList<entity.Customer> findCustomer(String name){
        	connect= Connector.getConnect();
		ArrayList<entity.Customer> list = new ArrayList<>();
		ResultSet rs=null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table+" where name LIKE '%"+name+"%'");
			rs=pstm.executeQuery();
			while(rs.next()){
				list.add(this.setCustomer(rs)); 
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		return list;
        
        }
        public ArrayList<entity.Customer> findIdCard(int id){
        	connect= Connector.getConnect();
		ArrayList<entity.Customer> list = new ArrayList<>();
		ResultSet rs=null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table+" where idcard=?");
                        pstm.setInt(1,id);
			rs=pstm.executeQuery();
			while(rs.next()){
				list.add(this.setCustomer(rs)); 
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		return list;
        
        }
        public void getByDate(String date){
        
        
        }
	private entity.Customer setCustomer(ResultSet rs) throws SQLException{
            entity.Customer customer = new entity.Customer();
            InfoBooking  info = new InfoBooking();
            customer.setId(rs.getInt("id"));
            customer.setName(rs.getString("name"));
            customer.setAddress(rs.getString("address")); 
            customer.setCountry(rs.getString("nation"));
            customer.setAge(rs.getInt("age"));
            customer.setGender(rs.getInt("gender"));
            customer.setId_card(rs.getInt("idcard"));
            customer.setId_team(rs.getInt("idteam"));
            customer.setPhone(rs.getString("phone"));
            customer.setInfo(info.getInfoBookingByCustomer(customer.getId()));
            return customer;
        }
	
}
